package com.pineapple.module.core.harvest.service;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.pineapple.module.core.harvest.domain.Harvest;
import com.pineapple.module.core.harvest.domain.HarvestCriteria;

public class HarvestManager {
	HarvestDAO dao = null;

	public HarvestManager(){
		dao = new HarvestDAO();
	}
	public List<Harvest> search(HarvestCriteria criteria,Connection conn)throws Exception{
		List<Harvest> lstHarvest = new ArrayList<Harvest>();
		int totalRacord =dao.count(criteria, conn);
		criteria.setTotalResult(totalRacord);

		if (criteria.getTotalResult() < criteria.getStart()) {
			criteria.setStart(criteria.getStart() - criteria.getLinePerPage());
		}
		if(totalRacord>0){
			lstHarvest = dao.search(criteria, conn);
		}
		return lstHarvest;
	}
	public Map<String,String> areaCombo(Connection conn)throws Exception{
		return dao.searchComboPlantAndArea(conn);
	}
	public void add(Connection conn,Harvest harvest)throws Exception{
		dao.add(conn, harvest);
		if(Integer.parseInt(harvest.getPlantingStatus())!=4 && Integer.parseInt(harvest.getPlantingStatus())!=6 ){
			dao.updateStatus(conn, harvest.getHarvestAreaId() ,Integer.parseInt(harvest.getPlantingStatus())+1);
		}
	}
	public void edit(Connection conn,Harvest harvest)throws Exception{
		dao.edit(conn, harvest);
	}

	public void delete(Connection conn,String harvestIds,String plantingId,int status)throws Exception{
		dao.delete(conn, harvestIds);
		dao.updateStatus(conn, plantingId , status-1);

	}
	public Harvest searchById(Connection conn,int id)throws Exception{
		return dao.searchById(conn, id);
	}

	public void updateStatus(Connection conn,String plantingId,int status) throws Exception{
		dao.updateStatus(conn, plantingId, status);
	}



}
